package com.chenbei.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenbei.system.domain.SysLoginLog;
import com.chenbei.system.mapper.SysLoginLogMapper;
import com.chenbei.system.service.ISysLoginLogService;
import com.chenbei.system.vo.req.SysLoginLogReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SysLoginLogService extends ServiceImpl<SysLoginLogMapper, SysLoginLog> implements ISysLoginLogService {

    @Override
    public Page<SysLoginLog> pageList(SysLoginLogReq sysLoginLogReq) {
        LambdaQueryWrapper<SysLoginLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(SysLoginLog::getId);
        if (StrUtil.isNotBlank(sysLoginLogReq.getLoginName())) {
            lambdaQueryWrapper.like(SysLoginLog::getLoginName, sysLoginLogReq.getLoginName());
        }
        if (sysLoginLogReq.getStatus() != null && sysLoginLogReq.getStatus() >= 0) {
            lambdaQueryWrapper.eq(SysLoginLog::getStatus, sysLoginLogReq.getStatus());
        }
        if (StrUtil.isNotBlank(sysLoginLogReq.getStartDate()) && StrUtil.isNotBlank(sysLoginLogReq.getEndDate())) {
            String start = sysLoginLogReq.getStartDate() + " 00:00:00";
            String end = sysLoginLogReq.getEndDate() + " 23:59:59";
            lambdaQueryWrapper.between(SysLoginLog::getCreateTime, start, end);
        }
        Page<SysLoginLog> pageConfig = new Page<>(sysLoginLogReq.getPageNum(), sysLoginLogReq.getPageSize());
        return page(pageConfig, lambdaQueryWrapper);
    }
}
